unit CallAnyDic;

interface
uses Windows, SysUtils, Classes, DicDM, IbDic, Authors, IzdatDic, PredmetDic, Grids, StdCtrls;

function SelectFromPredmetDic(aFilter: string): string;
procedure SelectFromAuthors(var aLastName, aFirstName: string);
function SelectFromIzdatDic(aFilter: string): string;
//
function SelectFromDic(TableName: string; var SelectedId: integer; var SelectedValue: variant): boolean;
procedure CallDic(aGrid: TStringGrid); overload;
procedure CallDic(aEdit: TEdit); overload;

implementation

procedure CallDic(aEdit: TEdit);
var dic: TibDicForm;
  entity: string;
begin
  Dic := TibDicForm.Create(nil);
  try
    entity := Copy(aEdit.Name, 4, 255);
    if entity[Length(entity)] in ['0'..'9'] then
      Delete(entity, Length(entity), 1);
    Dic.TableName := entity + '_DIC';
    Dic.Prepare(aEdit.Text, False);
    if Dic.ShowModal = idOk then begin
      // := pointer(Dic.Ds.FieldByName('ID').AsInteger);
      aEdit.Text := Dic.Ds.FieldByName('NAME').AsString;
    end;
  finally
    Dic.Free;
  end;

end;

procedure CallDic(aGrid: TStringGrid);
var dic: TibDicForm;
begin
  Dic := TibDicForm.Create(nil);
  try
    Dic.TableName := Copy(aGrid.Name, 3, 255) + '_DIC';
    Dic.Prepare(aGrid.Cells[aGrid.Col, aGrid.Row], False);
    if Dic.ShowModal = idOk then begin
      aGrid.Rows[aGrid.Row].Objects[0] := pointer(Dic.Ds.FieldByName('ID').AsInteger);
      aGrid.Cells[aGrid.Col, aGrid.Row] := Dic.Ds.FieldByName('NAME').AsString;
    end;
  finally
    Dic.Free;
  end;
end;

function SelectFromDic(TableName: string; var SelectedId: integer; var SelectedValue: variant): boolean;
var dic: TibDicForm;
begin
  result := False;
  Dic := TibDicForm.Create(nil);
  try
    Dic.TableName := TableName;
    Dic.Prepare('', False);
    if Dic.ShowModal = idOk then begin
      result := True;
      SelectedId := Dic.Ds.FieldByName('ID').AsInteger;
      SelectedValue := Dic.Ds.FieldByName('NAME').Value;
    end;
  finally
    Dic.Free;
  end;
end;


function SelectFromIzdatDic(aFilter: string): string;
var dic: TfmIzdatDic;
begin
  dic := TfmIzdatDic.Create(nil);
  try
    dic.Prepare(aFilter);
    if dic.ShowModal = idOk then
      result := dic.Ds.FieldByName('NAME').AsString;
  finally
    dic.Free;
  end;
end;

function SelectFromPredmetDic(aFilter: string): string;
var dic: TfmPredmetDic;
begin
  dic := TfmPredmetDic.Create(nil);
  try
    dic.Prepare(aFilter);
    if dic.ShowModal = idOk then
      result := dic.Ds.FieldByName('NAME').AsString;
  finally
    dic.Free;
  end;
end;

procedure SelectFromAuthors(var aLastName, aFirstName: string);
var dic: TfmAuthors;
begin
  dic := TfmAuthors.Create(nil);
  try
    dic.Prepare(aLastName);
    if dic.ShowModal = idOk then begin
      aLastName := dic.Ds.FieldByName('LAST_NAME').AsString;
      aFirstName := dic.Ds.FieldByName('FIRST_NAME').AsString;
    end;
  finally
    dic.Free;
  end;
end;

end.
